const { defineConfig } = require("@vue/cli-service");
const bodyParser = require("body-parser");
const userList = [
  {
    username: "admin",
    password: "admin",
  },
  {
    username: "123456",
    password: "123456",
  },
];
let systemCode = null

module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json());
      
      app.post("/api/login", (req, res) => {
        const { username, password } = req.body;
        const user = userList.find(
          (v) => v.username === username && password === v.password
        );
        if(user) {
          res.send({
            data: {
              token: 'FWEQWODQ;LWMD;CWLAKSDNLKVSK.ZWJ,DA;O/LNKAEJ,FNA;LW.D'
            },
            message: '登录成功'
          })
        } else {
          res.status(401).send({
            data: null,
            message: '账户密码错误, 请重新登录！'
          })
        }
      });

      app.post('/api/tel/login', (req, res) => {
        const { tel, code } = req.body;
        if(tel.length === 11 && code === systemCode) {
          res.send({
            data: {
              token: 'FLKAWMD;L/AWLDMA;LW.DMAW;LDK.AW LDQ.M,W'
            },
            message: '登录成功！'
          })
        } else {
          res.status(401).send({
            data: null,
            message: '手机号或者验证码不正确， 请重新输入！'
          })
        }
      })

      app.get('/api/tel/code', (req, res) => {
        systemCode = Math.random().toString(36).slice(2, 6)
        res.send({
          data: systemCode
        })
      })

      return middle;
    },
  },
});
